package com.bbkb.quartz.shiro;

import com.bbkb.common.constants.CommonConstant;
import com.bbkb.user.dto.SysUserDTO;
import com.bbkb.user.remoting.ISysUserRemoting;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 功能:shiro用户以及权限配置
 * 作者:沈飞超
 * 日期:2020年09月27日 6:08
 */
@Slf4j
public class AuthRealm extends AuthorizingRealm {

    @Autowired
    private ISysUserRemoting sysUserRemoting;
//    @Autowired
//    private ISysMenuService sysMenuService;
//    @Autowired
//    private ISysMenuBtnService sysMenuBtnService;

    /**
     * 为当限前登录的用户授予角色和权
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
//        String userName = (String) super.getAvailablePrincipal(principals);
//        SysUser sysUser = sysUserService.getSysUserByUserName(userName);
//
//        /*设置菜单和按钮权限*/
////        List<SysMenu> sysMenuList;
//        List<SysMenuBtn> sysMenuBtnList;
//        if (SuperAdminEnum.YES.getKey().equals(sysUser.getSuperAdmin())) {
////            sysMenuList = sysMenuService.getAllSysMenus();
//            sysMenuBtnList = sysMenuBtnService.getAllSysMenuBtns();
//        } else {
////            sysMenuList = sysMenuService.getSysMenusByUserId(sysUser.getId());
//            sysMenuBtnList = sysMenuBtnService.getMenuBtnsByUserID(sysUser.getId());
//        }
//        List<String> permList = new ArrayList<String>();
////        if (ArrayUtils.isNotEmpty(sysMenuList)) {
////            for (SysMenu sysMenu : sysMenuList) {
////                permList.add("menu:" + sysMenu.getMenuName() );
////            }
////        }
//
//        if (ArrayUtils.isNotEmpty(sysMenuBtnList)) {
//            for (SysMenuBtn sysMenuBtn : sysMenuBtnList) {
//                permList.add("btn:" + sysMenuBtn.getMenuName() + ":" + sysMenuBtn.getBtnName());
//            }
//        }
//        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
////        info.addRoles(roleList);
//        info.addStringPermissions(permList);
//        return info;
        return null;
    }

    /**
     * 验证当前登录的用户
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) authenticationToken;
        String userName = usernamePasswordToken.getUsername();
        SysUserDTO user = sysUserRemoting.getByUserName(userName);
        if (user == null) {
            throw new UnknownAccountException("用户不存在");
        }
        if (CommonConstant.NO == user.getStatus()) {
            throw new LockedAccountException("用户已禁用");
        }
        return new SimpleAuthenticationInfo(user.getUserName(), user.getPassword(), ByteSource.Util.bytes(user.getUserName()), getName());
    }

}
